package com.raozg.serializabletest;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 序列化测试
 * @Author: raozg
 * @date: 2022/2/17
 */
public class ObjectOutputStreamTest {
    public static void main(String[] args) {
        /**
         * 创建对象
         */
        Student s = new Student(1,"张三");

        ObjectOutputStream oos = null;
        try {
            /**
             * 序列化
             */
            oos = new ObjectOutputStream(new FileOutputStream("src/main/java/com/raozg/serializabletest/Student"));
            //序列化对象
            oos.writeObject(s);
            //序列化多个对象
            /**
             * 创建集合
             */
            List<Student> userList = new ArrayList<>();
            userList.add(new Student(1,"张三"));
            userList.add(new Student(2,"李四"));
            userList.add(new Student(3,"王五"));
            userList.add(new Student(4,"我"));
            /**
             * 创建对象
             */
            oos = new ObjectOutputStream(new FileOutputStream("src/main/java/com/raozg/serializabletest/Student"));
            /**
             * 序列化
             */
            oos.writeObject(userList);
            oos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
}
